﻿/// Copyright 2011 - Agus Kurniawan
/// Blog: http://blog.aguskurniawan.net 
/// ====================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace Galunggung
{
    public partial class MainForm : Form
    {
        private List<NicInfo> _listCurrentNic = null;
        private NicInfo _currentNic = null;
        private NicInfo _modifiedCurrentNic = null;
        private NicInfo _selectedMacAddress = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            TreeNode loading = new TreeNode("Loading...", 2, 2);
            treeNet.Nodes.Add(loading);
            toolStripAppStatus.Text = "Loading network adapters...";
            bckwLoading.RunWorkerAsync();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutGalunggung frm = new AboutGalunggung();
            frm.ShowDialog();
        }

        private void bckwLoading_DoWork(object sender, DoWorkEventArgs e)
        {
            _listCurrentNic = MacAddress.Synch();
        }

        private void bckwLoading_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            treeNet.Nodes.Clear();
            TreeNode root = new TreeNode("Network Adapters", 0, 0);
            root.Tag = null;
            for (int i = 0; i < _listCurrentNic.Count; i++)
            {
                TreeNode nic = new TreeNode(_listCurrentNic[i].Name, 1, 1);
                nic.Tag = _listCurrentNic[i];
                root.Nodes.Add(nic);
            }
            treeNet.Nodes.Add(root);
            treeNet.ExpandAll();
            toolStripAppStatus.Text = "Ready";            
        }

        private void treeNet_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selected = treeNet.SelectedNode;
            if (selected.Tag != null)
            {
                NicInfo info = (NicInfo)selected.Tag;
                lbNicTitle.Text = info.Name;
                lbStatusNetwork.Text = info.Status;
                lbIPAddress.Text = info.IPAddress;
                lbIpMask.Text = info.IPMask;
                txtOriginatedMac.Text = info.MACAdress;
                txtSpoofedMac.Text = info.SpoofedMACAdress;

                _currentNic = info;
                _modifiedCurrentNic = null;
                _selectedMacAddress = null;

                if (!string.IsNullOrEmpty(info.SpoofedMACAdress))                
                    btnRestore.Enabled = true;
                btnApply.Enabled = true;
                btnGenerate.Enabled = true;
                btnLookup.Enabled = true;
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            List<string> macs = Helper.GenerateAutoMacAddress();
            txtMac1.Text = macs[0];
            txtMac2.Text = macs[1];
            txtMac3.Text = macs[2];
            txtMac4.Text = macs[3];
            txtMac5.Text = macs[4];
            txtMac6.Text = macs[5];
        }

        private void btnLookup_Click(object sender, EventArgs e)
        {
            MacAddressLookup frm = new MacAddressLookup();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (frm.SelectSpoofedMacAddress!=null)
                {
                    _selectedMacAddress = frm.SelectSpoofedMacAddress;
                    string[] macs = _selectedMacAddress.SpoofedMACAdress.Split(new char[] { '-' });
                    txtMac1.Text = macs[0];
                    txtMac2.Text = macs[1];
                    txtMac3.Text = macs[2];
                    txtMac4.Text = macs[3];
                    txtMac5.Text = macs[4];
                    txtMac6.Text = macs[5];

                    string spoofedMac = string.Format("{0}-{1}-{2}-{3}-{4}-{5}", txtMac1.Text,
                    txtMac2.Text, txtMac3.Text, txtMac4.Text, txtMac5.Text, txtMac6.Text);

                    txtSpoofedMac.Text = spoofedMac;
                    btnActivate.Enabled = true;
                }
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            string spoofedMac = string.Format("{0}-{1}-{2}-{3}-{4}-{5}", txtMac1.Text,
                txtMac2.Text, txtMac3.Text, txtMac4.Text, txtMac5.Text, txtMac6.Text);

            if (!Helper.IsValidHexString(txtMac1.Text.ToCharArray()) || !Helper.IsValidHexString(txtMac2.Text.ToCharArray()) ||
               !Helper.IsValidHexString(txtMac3.Text.ToCharArray()) || !Helper.IsValidHexString(txtMac4.Text.ToCharArray()) ||
               !Helper.IsValidHexString(txtMac5.Text.ToCharArray()) || !Helper.IsValidHexString(txtMac6.Text.ToCharArray()))
            {
                MessageBox.Show("Mac Address is invalid", "Information");
                return;
            }

            txtSpoofedMac.Text = spoofedMac;
            _selectedMacAddress = null;
            btnActivate.Enabled = true;
        }

        private void btnActivate_Click(object sender, EventArgs e)
        {
            TreeNode selected = treeNet.SelectedNode;
            if (selected.Tag != null)
            {
                toolStripAppStatus.Text = "Spoofing....";
                Cursor = Cursors.WaitCursor;
                try
                {
                    NicInfo info = (NicInfo)selected.Tag;
                    if (_selectedMacAddress != null)
                    {
                        info.SpoofedMACAdress = _selectedMacAddress.SpoofedMACAdress;
                        info.SpoofedMACAdressId = _selectedMacAddress.SpoofedMACAdressId;
                    }
                    else
                    {
                        info.SpoofedMACAdress = txtSpoofedMac.Text;
                        info.SpoofedMACAdressId = 0;
                    }

                    MacAddress.Activate(info);
                    txtMac1.Text = "";
                    txtMac2.Text = "";
                    txtMac3.Text = "";
                    txtMac4.Text = "";
                    txtMac5.Text = "";
                    txtMac6.Text = "";
                    MessageBox.Show("Your Mac Address of network adapter was spoofed-;)", "Confirmation");
                }
                catch (Exception err)
                {
                    Cursor = Cursors.Default;
                    MessageBox.Show(err.Message, "Error");
                }
                Cursor = Cursors.Default;
                toolStripAppStatus.Text = "Ready";
                btnActivate.Enabled = false;
                btnRestore.Enabled = true;
            }
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            TreeNode selected = treeNet.SelectedNode;
            if (selected.Tag != null)
            {
                toolStripAppStatus.Text = "Restoring....";
                Cursor = Cursors.WaitCursor;
                try
                {

                    NicInfo info = (NicInfo)selected.Tag;
                    info.SpoofedMACAdress = "";
                    info.SpoofedMACAdressId = 0;

                    MacAddress.Restore(info);
                    txtSpoofedMac.Text = "";
                    txtMac1.Text = "";
                    txtMac2.Text = "";
                    txtMac3.Text = "";
                    txtMac4.Text = "";
                    txtMac5.Text = "";
                    txtMac6.Text = "";
                    MessageBox.Show("Your Mac Address of network adapter was restored", "Confirmation");
                }
                catch (Exception err)
                {
                    Cursor = Cursors.Default;
                    MessageBox.Show(err.Message, "Error");
                }
                Cursor = Cursors.Default;
                toolStripAppStatus.Text = "Ready";
                btnActivate.Enabled = false;
                btnRestore.Enabled = false;
            }
        }

        private void profilingNetworkAdapterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeNet.Nodes.Clear();
            TreeNode loading = new TreeNode("Loading...", 2, 2);
            treeNet.Nodes.Add(loading);
            toolStripAppStatus.Text = "Loading network adapters...";
            bckwLoading.RunWorkerAsync();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void checkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("http://galunggung.codeplex.com/");
            System.Diagnostics.Process.Start(startInfo);
        }

        private void howToUseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("http://blog.aguskurniawan.net/post/Getting-Startede28093Virtual-Mac-Address-10.aspx");
            System.Diagnostics.Process.Start(startInfo);
        }
    }
}
